package com.xtwy.encryption;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;

/**
 * @作 者: 侯建军
 * @日 期: 2018/9/1 11:29
 * @描 述:
 * @公 司：山西迅腾伟业
 */
public class Encryption {

    /**
     * base64 加密
     * @param code
     * @return
     */
    public static String jdkBase64Encoder (String code){
        String desc = Base64.getEncoder().encodeToString(code.getBytes(StandardCharsets.UTF_8));
        return desc;
    }

    /**
     * base64解密
     * @param code
     * @return
     */
    public static String jdkBase64Decoder(String code){
        String unDecodeStr=new String(Base64.getDecoder().decode(code),StandardCharsets.UTF_8);
        return unDecodeStr;
    }

    /**
     *  32位md5 散列加密
     * @param source
     * @return
     */
    public static String md5(String source) {
        StringBuffer sb = new StringBuffer(32);
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(source.getBytes("utf-8"));

            for (int i = 0; i < array.length; i++) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));
            }
        } catch (Exception e) {
            return null;
        }

        return sb.toString();
    }

    /**
     *  SHA1 散列加密
     * @param source
     * @return
     */
    public static String sha1(String source) {
        StringBuffer sb = new StringBuffer(32);
        try {
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] array = md.digest(source.getBytes("utf-8"));

            for (int i = 0; i < array.length; i++) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));
            }
        } catch (Exception e) {
            return null;
        }

        return sb.toString();
    }
    public static void  main(String[] args){
        String code="";
        code=jdkBase64Encoder("hello world");
        System.out.println(code);
        System.out.println(jdkBase64Decoder(code));
        //md5 散列加密
        System.out.println(md5("hello world"));
        //sha1 散列加密
        System.out.println(sha1("hello world"));
    }

//    //使用apache的commonsCodec实现
//    public static void commonsCodecBase64 (){
//        byte[] encodeBytes = org.apache.commons.codec.binary.Base64.encodeBase64(Base64Demo.src.getBytes());
//        String encode = new String (encodeBytes);
//        System.out.println("encode:  " + encode);
//
//        byte[] decodeBytes = org.apache.commons.codec.binary.Base64.decodeBase64(encode);
//        String decode = new String(decodeBytes);
//        System.out.println("decode:  " + decode);
//
//    }
//
//    //使用bouncyCastlede实现
//    public static void bouncyCastleBase64 () {
//        byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(Base64Demo.src.getBytes()) ;
//        String encode = new String (encodeBytes);
//        System.out.println("encode:  " + encode);
//
//        byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encode);
//        String decode = new String(decodeBytes);
//        System.out.println("decode:  " + decode);
//
//    }
}
